package com.atguigu.day02;

import com.atguigu.utils.ClickEvent;
import com.atguigu.utils.ClickSource;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

//MAP：将数据流中的每一个元素转换成另一个元素
public class Example4 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //匿名内部类创建方式
        //数据源->驱动map执行->驱动print执行 抽取名字
        env
                .addSource(new ClickSource())
                .map(new MapFunction<ClickEvent, String>() {
                    @Override
                    public String map(ClickEvent value) throws Exception {
                        return value.username;
                    }
                })
                .print();


        //外部类方式 创建MyMap类
        env
                .addSource(new ClickSource())
                .map(new MyMap())
                .print();


        //匿名函数方式
        env
                .addSource(new ClickSource())
                .map(r -> r.username)
                .print();


        //使用flatMap实现map的功能
        //语义：将每个元素转换成0个、1个、多个
        env
                .addSource(new ClickSource())
                .flatMap(new FlatMapFunction<ClickEvent, String>() {
                    @Override
                    public void flatMap(ClickEvent in, Collector<String> out) throws Exception {
                        out.collect(in.username);
                    }
                })
                .print();




        env.execute();
    }

    //创建外部类实现接口
    public static class MyMap implements MapFunction<ClickEvent,String>{
        @Override
        public String map(ClickEvent value) throws Exception {
            return value.username;
        }
    }


}
